package service;

import dao.CroUserDao;
import entity.CroUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by xuweijie on 17-5-3.
 */
@Service
public class CroUserService {
    @Autowired
    private CroUserDao croUserDao;

    /*
    * 通过账户名来查找后台用户
    * */
    @Transactional(readOnly = true)
    public CroUser findUserByName(String username){
        List<CroUser> croUserList=croUserDao.findByProperty("username",username);
        return croUserList.get(0);
    }

    /*
    * 根据id来查找用户
    * */
    @Transactional(readOnly = true)
    public CroUser FindUserById(String id){
        CroUser croUser=croUserDao.getById(id);
        return croUser;
    }

    /**
     * 查找全部用户
     * */

    @Transactional(readOnly = true)
    public List<CroUser> findAllUser(){
        String hql="from CroUser";
        List<CroUser> croUserList=croUserDao.findList(hql);
        return croUserList;
    }

    /*
    * 根据id删除用户
    * */
    @Transactional(readOnly = false)
    public boolean deleteUser(String id){
        try {
            croUserDao.delete(id);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /*
    * 更新用户信息
    * */
    @Transactional(readOnly = false)
    public boolean updateUser(CroUser croUser){
        try{
            croUserDao.update(croUser);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /*
    * 添加用户
    * */
    @Transactional(readOnly = false)
    public boolean insertUser(CroUser croUser){
        try {
            croUserDao.save(croUser);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

}
